package cn.lijiguo.concurrency.part1.background;

/**
 * MultiThreadsError
 * 线程安全问题 1 运行结果出错
 * 演示计数不准确（减少），找出具体出错的位置
 * @author lijiguo
 * @date 2020/1/7 16:52
 */
public class MultiThreadsError implements Runnable {

    int index = 0;

    static MultiThreadsError instance = new MultiThreadsError();

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(instance, "t1");
        Thread t2 = new Thread(instance, "t2");

        t1.start();
        t2.start();

        t1.join();
        t1.join();

        System.out.println(instance.index);
    }

    @Override
    public void run() {
//        while (index < 10000) {
//            index++;
//        }

        for (int i = 0; i < 10000; i++) {
            index++;
        }
    }
}
